package com.example.gobang.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class OnlineUserManager {
    // 此哈希表表示当前用户在 游戏大厅 的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> hall = new ConcurrentHashMap<>();
    // 此哈希表表示当前用户在 游戏房间 的在线状态
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<>();

    public void enterHall(int userId, WebSocketSession webSocketSession) {
        hall.put(userId, webSocketSession);
    }

    public void exitHall(int userId) {
        hall.remove(userId);
    }

    public WebSocketSession getFromHall(int userId) {
        return hall.get(userId);
    }

    public void enterRoom(int userId, WebSocketSession webSocketSession) {
        gameRoom.put(userId, webSocketSession);
    }

    public void exitRoom(int userId) {
        gameRoom.remove(userId);
    }

    public WebSocketSession getFromRoom(int userId) {
        return gameRoom.get(userId);
    }
}
